#include <stdio.h>

int bsearch(int* a, int length, int x) {
	int left = 0;
	int right = length - 1;

	while (left < right) {
		unsigned int middle = left + (right - left ) / 2;		
		if (a[middle] <= x) {
			left = middle + 1;
		} else {
			right = middle;
		}
	}
	
	if (a[left] > x) {
		return left;
	} else {
		return -1;
	}	
}

#define lngth 9

int main() {	
	int a[lngth] = {1, 1, 2, 2, 2, 4, 5, 5, 10};	

	for (int i = 0; i < lngth; i++) {
		printf("%d ", a[i]);
	}
	printf("\n");

	for (int i = 0; i < 12; i++) {
		printf("x = %d result = %d\n", i, bsearch(a, lngth, i));
	}
	
	return 0;	
}